import { type UserConfig, type ConfigEnv, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { wrapperEnv } from './load-env';
import AutoImport from 'unplugin-auto-import/vite';
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

export default ({ command, mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_PORT, VITE_BASE_URL } = wrapperEnv(env);
  return {
    plugins: [
      vue(),
      AutoImport({
        // 可以自定义文件生成的位置，默认是根目录下，使用ts的建议放src目录下
        dts: 'src/auto-imports.d.ts',
        imports: ['vue', 'vue-router', 'pinia'],
        eslintrc: {
          enabled: true,
        },
      }),
    ],
    resolve: {
      alias: [
        {
          find: '@',
          replacement: pathResolve('src') + '/',
        },
      ],
      dedupe: ['vue'],
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
          additionalData: `@import "src/styles/variable.scss";`,
        },
      },
    },
    server: {
      port: VITE_PORT,
      proxy: {
        '/api': {
          target: VITE_BASE_URL,
          changeOrigin: true,
        },
      },
    },
  };
};
